package com.paisheng.justlife.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.paisheng.justlife.R;
import com.paisheng.justlife.bean.Waste;
import com.paisheng.justlife.common.MenuType;

import java.util.List;

public class RubbishListAdapter extends BaseAdapter {

    private Context mContext;
    private List<Waste> wasteList;
    LayoutInflater inflater;
    private String deliver;
    public RubbishListAdapter(Context context, List<Waste> wasteList, String deliver) {
        this.mContext = context;
        this.wasteList = wasteList;
        this.deliver = deliver;
    }

    @Override
    public int getCount() {
        return wasteList.size();
    }

    @Override
    public Object getItem(int i) {
        return wasteList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder mHolder;
        if (convertView == null) {
            mHolder = new ViewHolder();
            inflater = LayoutInflater.from(mContext);
            convertView = inflater.inflate(R.layout.activity_item, null, true);
            mHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            mHolder.tv_quantity = (TextView) convertView.findViewById(R.id.tv_quantity);
            mHolder.tv_result_rice = (TextView) convertView.findViewById(R.id.tv_result_rice);
            mHolder.tv_total_money = (TextView) convertView.findViewById(R.id.tv_total_money);
            mHolder.im_img = (ImageView) convertView.findViewById(R.id.im_img);
            convertView.setTag(mHolder);
        } else {
            mHolder = (ViewHolder) convertView.getTag();
        }
        String name = wasteList.get(position).getName();
        String quantity = wasteList.get(position).getQuantity();
        String resultPrice = wasteList.get(position).getResultPrice();
        String totalMoney = wasteList.get(position).getTotalMoney();
        mHolder.tv_name.setText(name);
        if(deliver.equals(MenuType.DELIVER)){
            quantity = quantity+"公斤";
            resultPrice = "单价"+resultPrice+"元";
            totalMoney = "总价"+totalMoney+"元";
        }
        mHolder.tv_quantity.setText(quantity );
        mHolder.tv_result_rice.setText(resultPrice );
        mHolder.tv_total_money.setText(totalMoney);
        switch (name) {
            case (MenuType.METAL):
                mHolder.im_img.setImageResource(R.drawable.result_ic_metal);
                break;
            case (MenuType.PLASTIC):
                mHolder.im_img.setImageResource(R.drawable.result_ic_plastic);
                break;
            case (MenuType.PAPER):
                mHolder.im_img.setImageResource(R.drawable.result_ic_paper);
                break;
            case (MenuType.CLOTHES):
                mHolder.im_img.setImageResource(R.drawable.result_ic_clothes);
                break;
            case (MenuType.HARMFUL):
                mHolder.im_img.setImageResource(R.drawable.result_ic_harmful);
                break;
        }

        return convertView;
    }

    class ViewHolder {
        private ImageView im_img;
        private TextView tv_name;
        private TextView tv_quantity;
        private TextView tv_result_rice;
        private TextView tv_total_money;

    }
}
